// pages/shopInfo/shopInfo.js
import {
  HTTP
} from '../../utils/http-p.js'
const http = new HTTP;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    listImg:[
      { img: 'https://jintai-1252143811.cos.ap-beijing.myqcloud.com/images/wKimCV4K3XKANeFGAARQz4yxwHA561.jpg'},
      { img: 'https://jintai-1252143811.cos.ap-beijing.myqcloud.com/images/wKimCV2vvfmAE7AmAAaUXAEHlKk213.jpg'},
      { img: 'https://jintai-1252143811.cos.ap-beijing.myqcloud.com/images/wKimCV2vvgqABjZcAAm2qNPcU78246.jpg'},
      { img: 'https://jintai-1252143811.cos.ap-beijing.myqcloud.com/images/wKimCV2vvlWAPgfpAAohL5NH6u0560.jpg'},
      { img: 'https://jintai-1252143811.cos.ap-beijing.myqcloud.com/images/wKimCV2vvoWAJOU9AArvQCxC04g642.jpg'},
      { img: 'https://jintai-1252143811.cos.ap-beijing.myqcloud.com/images/wKimCV2vvpaAUQCqAAYrroAOTFM453.jpg'},
      { img: 'https://jintai-1252143811.cos.ap-beijing.myqcloud.com/images/wKimCV3BB5KAdFdjAAVS-Q-xp0Y275.jpg'}
    ],
    productImgList:[],
    productDescribeList:[],
    productParameterList:[],
    productInfo:{},
    productId:'',
    productQuantity:1
  },

  //预览图片
  previewImg: function (e) {
    var currentUrl = e.currentTarget.dataset.currenturl
    var previewUrls = e.currentTarget.dataset.previewurl
    wx.previewImage({
      current: currentUrl, //必须是http图片，本地图片无效
      urls: previewUrls, //必须是http图片，本地图片无效
    })
  },
  goToIndex() {
    wx.switchTab({url: '/pages/index/index'})
  },

  goToShopCar() {
    if (http.isLogin()) {
      return false
    }
    wx.navigateTo({ url: '/pages/shopCar/shopCar' });
  },


  /**
   * 查询商品详情 
   */
  getProductInfoById(id){
    http.request({
      url: `/product-info/info/${id}`,
      method:'post',
      success: (res) => {
        const data = res.data;
        console.log(data)
        this.setData({
          productInfo:data.productInfo,
          productDescribeList: data.productImgList,
          productImgList: data.productDescribeList,
          productParameterList: data.productParameterList
        })
      }
    })  
  },

  /**
   * 添加购物车
   */
  addProductCart(){
    if (http.isLogin()) {
      return false
    }
    const productId = this.data.productId
    let openId = wx.getStorageSync('openId')
    http.request({
      url: `product-cart/addProductCart/${productId}/${openId}`,
      method:'post',
      success: (res) => {
        const data = res.data;
        wx.showToast({
          title: '添加成功',
          icon: 'none'
        });
        console.log(data)
      }
    })  
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // var that = this
    // var picList = []
    // picList.push("https://jintai-1252143811.cos.ap-beijing.myqcloud.com/images/wKimCV3Iti6AaQEYAAb_SSl8tHQ974.jpg")
    // picList.push("https://jintai-1252143811.cos.ap-beijing.myqcloud.com/images/wKimCV3ItjeAYP8vAAQ4Y_Nb0s0799.jpg")
    // picList.push("https://jintai-1252143811.cos.ap-beijing.myqcloud.com/images/wKimCV3ItjiAPVxUAAObZVWQfNg996.jpg")
    // picList.push("https://jintai-1252143811.cos.ap-beijing.myqcloud.com/images/wKimCV3ItkCAW4c5AAI-zuaULp4459.jpg")
    // picList.push("https://jintai-1252143811.cos.ap-beijing.myqcloud.com/images/wKimCV3ItkCAY1XPAAJdl5CuKiM769.jpg")
    // that.setData({
    //   picList: picList,
    // })
    const productId = options.productId
    this.setData({
      productId
    })
    this.getProductInfoById(productId)
  },
  /**
   * 提交订单
   */
  onClickButton() {
    if (http.isLogin()) {
      return false
    }
    let submitList = [{
      productId: this.data.productId,
      productName: this.data.productInfo.productName,
      productPrice: this.data.productInfo.productPrice,
      productQuantity: this.data.productQuantity

    }]
    let sumPrice = this.data.productInfo.productPrice * this.data.productQuantity

    wx.navigateTo({
      url: "/pages/orderAccount/orderAccount?orderList=" + JSON.stringify(submitList) + "&sumPrice=" + sumPrice * 100
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})